import utils from './index.js'

var xhr = function (url, config, callback, error) {
    var xhr = new XMLHttpRequest(),
        data = config.data || {},
        params = utils.buildParams(data),
        type = config.type || 'GET',
        extra = config.extra;

    if(url.indexOf('?') === -1){
        url += '?' + params;
    }else{
        url += '&' + params;
    }

    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                var data = JSON.parse(xhr.responseText)
                callback(data, extra);
            } else {
                error && error();
            }
        }
    }

    //if(!data.hasOwnProperty('slot')){
    //    url = 'http://127.0.0.1:3001/?url=' + encodeURIComponent(url);
    //}

    xhr.open(type, url, true);
    xhr.send(null);
};

export default {
    xhr: xhr
}